package com.ccys.cms.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ccys.cms.service.SysCodeLogService;
import com.ccys.cms.service.SysCodeService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.cache.SysCodeUtil;
import com.ccys.db.dto.web.base.SysCodeDto;
import com.ccys.db.entity.SysCode;
import com.ccys.db.entity.SysCodeLog;
import com.ccys.db.mapper.SysCodeCategoryMapper;
import com.ccys.db.mapper.SysCodeMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.vo.administrator.SysCodeWebVO;
import com.ccys.db.vo.sysCodeCategory.SysCodeCategorySelectedListVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.ccys.common.vo.ResponseResult.success;
import static com.ccys.common.vo.ResponseResult.update;


@Service
public class SysCodeServiceImpl extends BaseServiceImpl<SysCode> implements SysCodeService {

    @Resource
    private SysCodeMapper sysCodeMapper;
    @Resource
    private SysCodeCategoryMapper sysCodeCategoryMapper;
    @Resource
    private SysCodeLogService sysCodeLogService;

    @Override
    public ResponseResult queryCodeList( SysCodeDto sysCodeDto) {
        PageDto.startPage();
        List<SysCodeWebVO> list = this.sysCodeMapper.listObjectsCms(sysCodeDto);
        PageInfo pageInfo = new PageInfo<>(list);
        return ResponseResult.success(pageInfo);
    }

    @Override
    public ResponseResult updateCode(SysCode sysCode) {
        int i = this.insertOrUpdate(sysCode);
        SysCode code = this.findEntityById(sysCode.getId());
        SysCodeUtil.updateCode(SysCodeUtil.UPDATE,code);
        if(i > 0) {
            SysCodeLog sysCodeLog = new SysCodeLog();
            BeanUtils.copyProperties(sysCode, sysCodeLog);
            sysCodeLog.setId(IdUtil.randomUUID());
            this.sysCodeLogService.saveEntity(sysCodeLog);
        }
        return update(i);
    }

    @Override
    public ResponseResult<SysCodeCategorySelectedListVo,Object> listCategory() {
        List<SysCodeCategorySelectedListVo> list = this.sysCodeCategoryMapper.listCategory();
        return success(list);
    }
}
